<html>
<head><meta charset="utf-8"><title>Boxed DST with FFI · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Boxed.20DST.20with.20FFI.html">Boxed DST with FFI</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="240168511"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Boxed%20DST%20with%20FFI/near/240168511" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Boxed.20DST.20with.20FFI.html#240168511">(May 25 2021 at 10:36)</a>:</h4>
<p>Hello!</p>
<p>I have been trying to understand the memory layout of boxed DST and if they are valid in FFI context. To that end I have just tried those small examples in the playground and am relying on the <code>improper_ctypes_definitions</code> lint to guide me:</p>
<ul>
<li><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=be3462c0bff5494fdac59e85c0f1a0a3">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=be3462c0bff5494fdac59e85c0f1a0a3</a></li>
</ul>
<p>It seems that when boxed, both sized and dynamically sized types are valid in FFI context. However when reading the documentation at <a href="https://doc.rust-lang.org/std/boxed/index.html">https://doc.rust-lang.org/std/boxed/index.html</a> there are no explicit mentions of the validity of boxed DST in FFI context.</p>
<p>Therefore, I've been wondering what are the actual rules for boxed DST through FFI and if they are valid, what's the memory layout so that I can properly manipulate such types in C.</p>



<a name="240169063"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Boxed%20DST%20with%20FFI/near/240169063" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Boxed.20DST.20with.20FFI.html#240169063">(May 25 2021 at 10:43)</a>:</h4>
<p>And as one might have guess, the size in memory of <code>Box&lt;str&gt;</code> and <code>Box&lt;[u8]&gt;</code> is <code>16</code> so if they are valid in FFI context, could I assume the memory layout is ptr+len?</p>



<a name="240169806"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Boxed%20DST%20with%20FFI/near/240169806" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Boxed.20DST.20with.20FFI.html#240169806">(May 25 2021 at 10:52)</a>:</h4>
<p>Fat pointer layout is not stable,  you have to use <code>into_raw</code> and then <code>len()</code> and <code>as_ptr()</code> to get the components.</p>



<a name="240170045"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Boxed%20DST%20with%20FFI/near/240170045" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Boxed.20DST.20with.20FFI.html#240170045">(May 25 2021 at 10:55)</a>:</h4>
<p>Should the <code>improper_ctypes_definitions</code> lint complain then?</p>



<a name="240292650"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Boxed%20DST%20with%20FFI/near/240292650" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Boxed.20DST.20with.20FFI.html#240292650">(May 26 2021 at 08:27)</a>:</h4>
<p>I have opened <a href="https://github.com/rust-lang/rust/issues/85714">https://github.com/rust-lang/rust/issues/85714</a> to continue the discussion.</p>



<a name="241680795"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Boxed%20DST%20with%20FFI/near/241680795" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Boxed.20DST.20with.20FFI.html#241680795">(Jun 06 2021 at 08:37)</a>:</h4>
<p>The lint has been improved and now it complains in nightly<br>
<a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=aae6de297f4ed73beacb1ac8f7c4c2df">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=aae6de297f4ed73beacb1ac8f7c4c2df</a><br>
<span aria-label="tada" class="emoji emoji-1f389" role="img" title="tada">:tada:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>